package com.pethub.controller.web;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import org.springframework.validation.BindingResult;
import org.springframework.web.multipart.MultipartFile;

public class fileUpload {
	MultipartFile multipartFile;
	public MultipartFile getMultipartFile() {
		return multipartFile;
	}
	public void setMultipartFile(MultipartFile multipartFile) {
		this.multipartFile = multipartFile;
	}
	public fileUpload() {
		
	}
	public fileUpload(fileUpload uploadedFile,BindingResult result,fileUploadValidator fileValidator) {
		// TODO Auto-generated constructor stub
		InputStream inputStream = null;
		  OutputStream outputStream = null;
		 
		  // Getting uploaded file
		  MultipartFile file = uploadedFile.getMultipartFile();
		  fileValidator.validate(uploadedFile, result);
		 
		  String fileName = file.getOriginalFilename();
		 
		  try {
			   inputStream = file.getInputStream();
			 
			   File newFile = new File("/statics/file/" + fileName);
			   if (!newFile.exists()) {
			    newFile.createNewFile();
			   }
			   outputStream = new FileOutputStream(newFile);
			   int read = 0;
			   byte[] bytes = new byte[1024];
			 
			   while ((read = inputStream.read(bytes)) != -1) {
			    outputStream.write(bytes, 0, read);
			   }
			   
			  } catch (IOException e) {
			   // TODO Auto-generated catch block
			   e.printStackTrace();
		  }
	}

}
